package 领扣算法.力扣计划.算法基础.另一棵树的子树;


  class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode() {}
      TreeNode(int val) { this.val = val; }
      TreeNode(int val, TreeNode left, TreeNode right) {
          this.val = val;
          this.left = left;
          this.right = right;
      }
  }

class Main {
      boolean res=false;
    public boolean isSubtree(TreeNode root, TreeNode subRoot) {
       plus(root,subRoot);
       return res;
    }

    private void plus(TreeNode root, TreeNode subRoot) {
        //执行先根遍历
        if (root==null||res)
            return;
        if (root.val==subRoot.val)
            if (plus2(root,subRoot))
                res=true;
        plus(root.left,subRoot);
        plus(root.right,subRoot);

    }

    private boolean plus2(TreeNode root, TreeNode subRoot) {
        if (root==null&&subRoot==null)
            return true;
        if (root==null||subRoot==null)
            return false;
        if(root.val!=subRoot.val)
        {
            return false;
        }
        return plus2(root.left,subRoot.left)&&plus2(root.right,subRoot.right);
    }

}